<?php
class UserController extends BaseApiController
{

    function init()
    {
        parent::init();
    }

    #首页
    function indexAction(){
        $uid = $this->uid; //base接口中需要用户信息,没有用户信息不会访问到接口
        $app_version = !empty($_REQUEST['app_version']) ? $_REQUEST['app_version'] : '';
        $data = array();
        $flag = 0;

        #获取我的最新位置
        $new_location_model = new NewLocationModel();
        $data['now_location'] = $new_location_model->getDataByUid($uid);

        #获取好友的位置
        $user_list_model = new UserListModel();
        $user_model = new UserModel();
        $user_info = $user_model->getUserInfoByUid($uid);
        if($user_info['member'] == 1){
            $flag = 1;
        }

        $condition = array(
            'uid' => $uid,
            'status' => 2, //好友状态
        );
        $friends = $user_list_model->getListByAdmin(1, 50, $condition);
        if(!empty($friends)){
            foreach($friends as $friend){
                if(!$flag){ //本人不是会员
                    $user_info = $user_model->getUserInfoByUid($friend['friend_id']);
                    if(empty($user_info['member'])){
                        continue;
                    }
                }
                if($friend['side_hide'] == 2){ //隐藏,则不查询位置
                    continue;
                }
                $location = $new_location_model->getDataByUid($friend['friend_id']);
                if(empty($location)){
                    $friend['now_location'] = array();
                }else{
                    $friend['now_location'] = $location;
                }

                if(!empty($friend)){
                    $friend['avatar'] = !empty($friend['avatar']) ? QINIU_DOMAIN.$friend['avatar'] : '';
                }
                $data['friends'][] = $friend;
            }
        }


//        if($app_version <= 1.2){
//            $data['update'] = array(
//                'type' => '2', //1-强制更新,2-不强制更新
//                'update_url' => '', //更新链接
//            );
//        }
        return $this->echo_message(self::RETURN_SUCCESS_MSG,self::RETURN_SUCCESS_CODE, $data);
    }

    #好友列表
    function friendListAction(){
        $uid = $this->uid; //base接口中需要用户信息,没有用户信息不会访问到接口
        $page_size = isset($_REQUEST['page_size']) ? intval($_REQUEST['page_size']) : 20; //一页展示多少
        $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1; //页码


        $user_list_model = new UserListModel();
        $condition = array(
            'uid' => $uid,
            'status' => 2
        );
        $friend_list = $user_list_model->getListByAdmin($page, $page_size, $condition);

        $user_model = new UserModel();
        $my_info = $user_model->getUserInfoByUid($uid);
        $flag = 0;
        if($my_info['member'] == 1){
            $flag = 1;
        }

        if(!empty($friend_list)){
            $new_location_model = new NewLocationModel();
            $user_model = new UserModel();
            foreach($friend_list as $key => $val){
                $user_info = $user_model->getUserInfoByUid($val['friend_id']);

                $new_location = $new_location_model->getDataByUid($val['uid']);
                $friend_list[$key]['destination'] = $new_location['destination'];
                $friend_list[$key]['dest_time'] = $new_location['updated_at'];
                $friend_list[$key]['mobile'] = $user_info['mobile'];
                $friend_list[$key]['nickname'] = $val['remark'];
                $friend_list[$key]['longitude'] = $new_location['longitude'];
                $friend_list[$key]['latitude'] = $new_location['latitude'];
                $friend_list[$key]['avatar'] = !empty($friend_list[$key]['avatar']) ? QINIU_DOMAIN . $friend_list[$key]['avatar'] : '';
                if(!$flag && empty($user_info['member'])){
                    $friend_list[$key]['destination'] = substr($new_location['destination'], 0, 9)."***(开通VIP获取更详细的位置)";
                }
            }
        }

        return $this->echo_message(self::RETURN_SUCCESS_MSG,self::RETURN_SUCCESS_CODE, $friend_list);
    }

    #发送好友请求
    function inviteAction(){
        $uid = $this->uid; //base接口中需要用户信息,没有用户信息不会访问到接口
        $mobile = isset($_REQUEST['mobile']) ? intval($_REQUEST['mobile']) : '0';

        if(empty($mobile)){
            $code = 10010; //参数错误
            $msg = $this->_error_msg->system_errors[$code];
            return $this->echo_message($msg,$code);
        }

        $redis_key = 'invite_' . $uid . '_' . $mobile;
        $redis_model = new RedisModel();
        $re_invite = $redis_model->getData($redis_key);
        if($re_invite){ //一天发一次好友请求
            return $this->echo_message(self::RETURN_SUCCESS_MSG,self::RETURN_SUCCESS_CODE);
        }

        #确认用户
        $user_model = new UserModel();
        $my_info = $user_model->getUserInfoByUid($uid);
        if(!$my_info){
            $code = 20049; //用户不存在
            $msg = $this->_error_msg->system_errors[$code];
            return $this->echo_message($msg,$code);
        }

        $user_info = $user_model->getUserInfoByMobile($mobile);
        if(!$user_info){
            $code = 20049; //用户不存在
            $msg = $this->_error_msg->system_errors[$code];
            return $this->echo_message($msg,$code);
        }

        $user_list_model = new UserListModel();
        $invite_info = $user_list_model->getFriends($uid, $user_info['uid']);
        if(!empty($invite_info) && $invite_info['type'] == 2){
            $code = 20046; //已经是好友了
            $msg = $this->_error_msg->system_errors[$code];
            return $this->echo_message($msg,$code);
        }

        $notice_model = new NoticeModel();
        $add_data = array(
            'uid' => $user_info['uid'],
            'title' => '好友请求',
            'type' => 2,
            'isnew' => 1,
            'friend_id' => $uid,
            'note' => "用户" . $my_info['nickname'] . "请求添加您为好友",
            'created_at' => time()
        );
        $ret = $notice_model->addData($add_data);
        if($ret){
            $redis_model->setData($redis_key, 1, strtotime(date('Ymd',strtotime("+1 day")))-time());//一天内只能请求一次s

            #给被添加好友的人推送
            if(!empty($user_info['push_token'])){
                $push_msg_model = new PushMsg();  //友盟
                $push_msg_model->addPushOfInvite($my_info['nickname'] ,$user_info['push_token']);
            }

            return $this->echo_message(self::RETURN_SUCCESS_MSG,self::RETURN_SUCCESS_CODE);
        }else{
            $code = 20045; //添加失败,请重试
            $msg = $this->_error_msg->system_errors[$code];
            return $this->echo_message($msg,$code);
        }
    }

    #搜索好友是否注册账号
    function searchFriendAction(){
        $uid = $this->uid; //base接口中需要用户信息,没有用户信息不会访问到接口
        $mobile = isset($_REQUEST['mobile']) ? intval($_REQUEST['mobile']) : '0';

        if(empty($mobile)){
            $code = 10010; //参数错误
            $msg = $this->_error_msg->system_errors[$code];
            return $this->echo_message($msg,$code);
        }

        $user_model = new UserModel();
        $user_info = $user_model->getUserInfoByMobile($mobile);
        if(!$user_info){
            $code = 20002; //用户不存在
            $msg = $this->_error_msg->system_errors[$code];
            return $this->echo_message($msg,$code);
        }

        if(!empty($user_info) && $user_info['uid'] == $uid){
            $code = 20047; //不可以添加自己
            $msg = $this->_error_msg->system_errors[$code];
            return $this->echo_message($msg,$code);
        }

        return $this->echo_message(self::RETURN_SUCCESS_MSG,self::RETURN_SUCCESS_CODE, $user_info);
    }

    #处理好友邀请，接受
    function receiveAction(){
        $uid = $this->uid; //base接口中需要用户信息,没有用户信息不会访问到接口
        $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : '0';

        if(empty($id)){
            $code = 10010; //参数错误
            $msg = $this->_error_msg->system_errors[$code];
            return $this->echo_message($msg,$code);
        }

        $notice_model = new NoticeModel();
        $invite_info = $notice_model->getDataById($id);

        if(!empty($invite_info) && $invite_info['type'] == 2 && $invite_info['isnew'] == 1 && !empty($invite_info['friend_id'])){//需要加好友

            $user_list_model = new UserListModel();
            $ret = $user_list_model->makeFriends($uid, $invite_info['friend_id']);
            if(!$ret){
                $code = 20045; //添加失败,请重试
                $msg = $this->_error_msg->system_errors[$code];
                return $this->echo_message($msg,$code);
            }

            $update_status = array('isnew'=>2);
            $ret = $notice_model->updateDataById($update_status, $id);

            return $this->echo_message(self::RETURN_SUCCESS_MSG,self::RETURN_SUCCESS_CODE);
        }else{

            if(!empty($invite_info) && $invite_info['type'] == 2 && $invite_info['isnew'] == 2 ){
                $code = 20046; //已经是好友了
                $msg = $this->_error_msg->system_errors[$code];
                return $this->echo_message($msg,$code);
            }
        }

        $code = 10010; //参数错误
        $msg = $this->_error_msg->system_errors[$code];
        return $this->echo_message($msg,$code);
    }

    #编辑好友信息
    function editFriendAction(){
        $uid = $this->uid; //base接口中需要用户信息,没有用户信息不会访问到接口
        $friend_id = isset($_REQUEST['friend_id']) ? intval($_REQUEST['friend_id']) : '0'; //好友uid
        $status =  isset($_REQUEST['status']) ? intval($_REQUEST['status']) : '0'; //是否接受成为好友,1-单向好友申请， 2-互相成为好友， 3-拒绝/解除好友关系
        $remark =  isset($_REQUEST['remark']) ? trim($_REQUEST['remark']) : ''; //remark
        $hide =  isset($_REQUEST['hide']) ? intval($_REQUEST['hide']) : '';

        if(empty($friend_id)){
            $code = 10010; //参数错误
            $msg = $this->_error_msg->system_errors[$code];
            return $this->echo_message($msg,$code);
        }

        $update = $infor_fri_update = array();
        $user_list_model = new UserListModel();
        if(!empty($remark)){
            $update['remark'] = $remark;
        }
        if(!empty($hide)){
            $update['hide'] = $hide;
            $infor_fri_update['side_hide'] = $hide;
        }

        if(!empty($status)) {
            $update['status'] = $status;
            $infor_fri_update['status'] = $status;
        }

        #对方的好友关系解绑,是否被隐藏位置
        if(!empty($infor_fri)){
            $infor_fri = $user_list_model->getFriends($friend_id, $uid);
            $user_list_model->updateDataById($infor_fri_update, $infor_fri['id']);
        }

        if(!empty($update)){
            #自己这边好友资料更新
            $infor = $user_list_model->getFriends($uid, $friend_id);
            $user_list_model->updateDataById($update, $infor['id']);
        }

        return $this->echo_message(self::RETURN_SUCCESS_MSG,self::RETURN_SUCCESS_CODE);
    }


    #通知消息列表。分为系统消息和好友消息
    function noticeAction(){
        $uid = $this->uid; //base接口中需要用户信息,没有用户信息不会访问到接口
        $page_size = isset($_REQUEST['page_size']) ? intval($_REQUEST['page_size']) : 20; //一页展示多少
        $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1; //页码
        $type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) == 1 ? '1,4' : '2,3' : '2,3'; //类型 1-系统消息,2-添加好友消息,3-消息,23放一起显示

        #查询好友
        $condition = array(
            'uid' => $uid,
            'type' => $type
        );
        $notice_model = new NoticeModel();
        $user_model = new UserModel();
        $data = $notice_model->getListByAdmin($page, $page_size, $condition);
        foreach($data as $key => $val){
            $user_info = $user_model->getUserInfoByUid($val['friend_id']);
            $data[$key]['avatar'] =  !empty($user_info['avatar']) ? QINIU_DOMAIN . $user_info['avatar'] : '';
            $data[$key]['nickname'] =  $user_info['nickname'];
        }

        #查询过一次消息,就将所有消息设置为已读
        $update = array(
            'isnew' => 2
        );
        $notice_model->updateIsnewByUid($update, $uid);

        return $this->echo_message(self::RETURN_SUCCESS_MSG,self::RETURN_SUCCESS_CODE,$data);
    }


}